<template>
  <div class="cremation_statistics">
    <div class="PageContent">
      <el-form
        :model="ruleForm"
        :inline="true"
        ref="ruleForm"
        label-width="100px"
        :rules="rules"
      >
        <div class="PageItem margintop20">
          <el-row >
            <!-- <el-form-item label="时间" prop="date">
              <el-date-picker
                v-model="ruleForm.date"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change='dateChange'
              >
              </el-date-picker>
            </el-form-item> -->
             <el-form-item label="开始日期:" prop="appointmentDateStart">
              <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.appointmentDateStart"
                              format="yyyy-MM-dd" :picker-options="pickerOptions"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期:" prop="appointmentDateEnd">
              <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.appointmentDateEnd"
                              format="yyyy-MM-dd" :picker-options="pickerOptions"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button v-if="btnRight.btnSearch" type="primary" @click="submitForm('ruleForm')">查询</el-button>
            </el-form-item>
          </el-row>
          <!-- <el-button type="primary" @click="readDeadIdCard">读卡</el-button> -->
        </div>
      </el-form>
      <div class="PageItem">
          <div id="myCharts" ref="myCharts"></div>
      </div>
      <div id="idCardContainer"></div>
      <div style="width: 0px; height: 0px; overflow: hidden">
      <object id="view1" type="application/x-eloamplugin" name="view"></object>
    </div>
    </div>
  </div>
</template>

<script>
import {dataFormatetmd} from '@/utils/index'
import * as API from '@/api/civilAffairsBureau/index'
 import IdCard from "@/api/IdCard_SS628_100_001U";
export default {
  data() {
    return {
      myCharts:null,
      chartsData:{},
      ruleForm: {
        appointmentDateStart: "",//开始时间
        appointmentDateEnd:'',//结束时间
      },
      rules:{
        appointmentDateStart:[
          { required: true, message: '请输入开始时间', trigger: 'blur' }
        ],
        appointmentDateEnd:[
          { required: true, message: '请输入结束时间', trigger: 'blur' }
        ]
      },
       pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
      },
      //按钮权限
        btnRight: {
          btnSearch: false,
         
        }, 
    };
  },
  mounted(){
    //初始化时间
    this.defaultValue()
    //初始化图表
    this.initMyCharts();
    //获取数据
    this.getData()
    //按钮权限
      this.btnRight = this.$route.meta.buttons;
      //遍历属性赋值将true与false字符串转换成布尔型的值
      for (var key of Object.keys(this.btnRight)) {
        this.btnRight[key] =
          this.btnRight[key] || this.btnRight[key] == "true" ? true : false;
      }
  },
  methods:{
     //默认初始值方法
    defaultValue() {
      //默认查询开始结束时间为当天
      this.ruleForm.appointmentDateStart = dataFormatetmd(new Date());
      this.ruleForm.appointmentDateEnd = dataFormatetmd(new Date());
    },
    //查询
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.getData()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      //请求数据
      async getData(){
        this.myCharts.showLoading()
        let params={
          appointmentDateStart:dataFormatetmd(new Date(this.ruleForm.appointmentDateStart)),
          appointmentDateEnd:dataFormatetmd(new Date(this.ruleForm.appointmentDateEnd)),
        }
        const res = await API.findFuneralCremation(params)
        if(res.code==1){
          this.chartsData=res.data;
          this.updatedMyCharts()
        }else{
          this.$message.error('数据读取失败');
        }
      },
      dateChange(val){
        
      },
    //初始化图表
    initMyCharts(){
      this.myCharts=this.$echarts.init(this.$refs.myCharts);
      const initOption={
            grid:{
              left:50,
              top:30,
              right:20,
              bottom:100
            },
            tooltip:{

            },
            legend: {
              show:true,
              top:0,
              right:0,
              data:[
                '火化量'
              ]
            },
            xAxis: {
              type:'category',
              axisLabel: { //坐标轴刻度标签的相关设置。
                textStyle: {
                  color: '#078ceb',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12,
                },
                rotate:50,
              },
            },
            yAxis: {
             
            },
            series: [
              ]
      }
      this.myCharts.setOption(initOption)
    },
    updatedMyCharts() {
      const dataOption = {
        xAxis:{
          data:this.chartsData.names,
        },
        series:[
          {
            
                name: '火化量',
                type: 'bar',
                barWidth:30,
                itemStyle:{
                 normal: {
                      color: (params) => {
                          let colors = ['#4150d8', '#28bf7e', '#ed7c2f', '#f2a93b', '#f9cf36', '#4a5bdc', '#FF3E96', '#ffe180', '#b6c2ff', '#79CDCD','#68228B', '#008B00','#8B5A2B','#EE3A8C','#A52A2A','#FFC1C1' ]
                          return colors[params.dataIndex]
                      },
                      barBorderRadius:[15,15,0,0]
                  }
                },
                data: this.chartsData.numbers
            }
          
        ]
      }
      this.myCharts.setOption(dataOption)
       this.myCharts.hideLoading();
    },
  }
};
</script>

<style lang="scss" scoped>
.cremation_statistics {
  width: 100%;
  #myCharts{
    width: 100%;
    height: 600px;
  }
}
</style>